/****************************************************************************
 Copyright (c) 2012-2012 lijiejun
 Copyright (c) 2010-2012 cocos2d-x.org
 Copyright (c) 2008-2010 Ricardo Quesada
 Copyright (c) 2011      Zynga Inc.
 
 http://www.cocos2d-x.org
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ****************************************************************************/

package org.cocos2d.platform
{
	import flash.display.Sprite;
	import flash.display3D.Context3D;
	import flash.display3D.Context3DCompareMode;
	import flash.display3D.Context3DRenderMode;
	import flash.display3D.Context3DTriangleFace;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	import org.cocos2d.platform.LanguageType;
	
	public class CCApplication extends Sprite implements CCApplicationProtocol
	{
		public function CCApplication()
		{
			if (sm_pSharedApplication != null) throw new Error("sm_pSharedApplication is not null");
			sm_pSharedApplication = this;
			addEventListener(Event.ADDED_TO_STAGE, addedToStage);
			context3D = null;
			renderMode = Context3DRenderMode.AUTO;
			frameRate = 60;
		}
		
		public function applicationDidFinishLaunching() : Boolean
		{
			return false;
		}
		
		public function applicationDidEnterBackground() : void
		{
		}
		
		public function applicationWillEnterForeground() : void
		{
		}
		
		public function setAnimationInterval(interval : Number) : void
		{
		}
		
		public function getCurrentLanguage() : uint
		{
			return LanguageType.kLanguageEnglish;
		}
		
		public function isIpad() : Boolean
		{
			return false;
		}
		
		public static function sharedApplication() : CCApplication
		{
			if (!sm_pSharedApplication) throw new Error("sm_pSharedApplication is null");
			return sm_pSharedApplication;
		}
		
		protected static var sm_pSharedApplication : CCApplication;
		
		/* Stage3D related functions and variables*/
		protected var context3D : Context3D;
		protected var frameRate : uint;
		protected var renderMode : String;
		
		protected var antialiasing : uint = 2;
		
		protected var enableErrorChecking : Boolean = false;
		protected var bounds : Rectangle;
		
		protected function addedToStage(event : Event) : void
		{
			removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
			stage.addEventListener(Event.RESIZE, resizeStage);
			stage.frameRate = frameRate;
			stage.stage3Ds[0].addEventListener(Event.CONTEXT3D_CREATE, context3DCreated);
			stage.stage3Ds[0].addEventListener(ErrorEvent.ERROR, context3DError);
			stage.stage3Ds[0].requestContext3D(renderMode);
		}
		
		protected function context3DCreated(e : Event) : void
		{
			context3D = stage.stage3Ds[0].context3D;
			context3D.enableErrorChecking = enableErrorChecking;
			context3D.setCulling(Context3DTriangleFace.NONE);
			context3D.setDepthTest(false, Context3DCompareMode.ALWAYS);
			
			resizeStage();
		}
		
		protected function resizeStage(e : Event = null) : void
		{
			if (!context3D) return;
			var rect : Rectangle = bounds ? bounds : new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
			stage.stage3Ds[0].x = rect.x;
			stage.stage3Ds[0].y = rect.y;
			
			context3D.configureBackBuffer(rect.width, rect.height, antialiasing, false);
		}
		
		public function get Context() : Context3D
		{
			return context3D;
		}
		
		protected function context3DError(e : ErrorEvent) : void
		{
			throw new Error("The SWF is not embedded property. The 3D context can't be created. Wrong WMODE? Set it to 'direct'.");
		}
	}
}